package com.miao.test.support;

import com.miao.test.common.Message;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/5/28 16:47
 * @Description:
 */
public class SocketUtil {

    public final static Map<Long, SocketListener> listenerMap = new ConcurrentHashMap<>();

    public interface SocketListener {
        void callback(Message respMsg);
    }

    public static void write(Channel channel, Message message, SocketListener listener) {
        BinaryWebSocketFrame binaryWebSocketFrame = new BinaryWebSocketFrame(message.getByteBuf());
        channel.writeAndFlush(binaryWebSocketFrame);
        if (listener != null) {
            listenerMap.put(message.getSequence(), listener);
        }
    }
}
